# huOnAir Makefile
######################
# grabbed from ShowIP:
######################
#
#    ShowIP Firefox Extension
#    Copyright (C) 2007 Jan Dittmer <jdi@l4x.org>
#    GPLv2 only

################################################################################
# Input Validation

ifeq ($(MAKECMDGOALS),package)
  ifndef channel
    $(error when making a package, you must specify the channel! \
            'beta' or 'public'!)
  endif
endif


# version should be X.Y.Z for release, X.Y.Z+1pre.DATEa for beta

CHANGESET = $(shell hg tip | grep changeset | sed 's/changeset: *//')
REVISION = $(shell hg id -n)

MAJORVERSION = $(shell cat major.ver)
MINORVERSION = $(shell cat minor.ver)
BUILDVERSION = $(shell cat build.ver)

BETABUILDVERSION = $(shell expr 1 + $(BUILDVERSION))
BETAPOSTFIX = $(shell date +%Y%m%d)a

VERSIONPREFIX = $(MAJORVERSION).$(MINORVERSION)

BETAVERSION = $(VERSIONPREFIX).$(BETABUILDVERSION)pre.$(BETAPOSTFIX)
DEVVERSION = $(VERSIONPREFIX).$(BETABUILDVERSION)pre.dev

PUBLICVERSION = $(VERSIONPREFIX).$(BUILDVERSION)



# Development Channel
ifeq ($(channel),beta)
  # Development build updates are managed by the website, so we construct
  # an update URL that points to the update manifest we are going to create.
VERSION     := $(BETAVERSION)

# Release Channel
else ifeq ($(channel),public)
  # Release build updates are managed by AMO, which provides its own update.
VERSION     := $(PUBLICVERSION)

# No Channel
else ifdef channel
  $(error your channel is unknown/unsupported: $(channel))
endif



#######

all: help

buildxpi: buildbefore buildcenter buildafter

buildbefore:
	@for f in install.rdf; do \
		sed "s/MRONAIRVERSION/$(VERSION)/g" $$f.template \
			> $$f; \
	done
buildafter: buildcenter
	@echo Version: $(VERSION)
	@mv $(FILENAME).xpi $(FILENAME)-$(VERSION).xpi
#	for f in install.rdf; do \
#		hg revert $$f; \
#		rm $$f.orig; \
#		hg revert $$f; \
#	done
	

buildcenter: buildbefore
	. ./build.sh
#	set
override FILENAME = huonair-mronair
ifeq ($(FILENAME),)
	@echo "APP_NAME was not exported by build script!"
	@false;
endif

	@rm -f app_name.txt || true

package: 
	@echo Version: $(VERSION)
	hg pull || true && hg up || true
	@make buildxpi

releasebeta: buildbeta
	read "Are you sure you want to pack release $(VERSION)??" || true
	if hg diff | grep @@ >/dev/null; then echo "There are changes, won't make a release"; false; fi
#	if svn diff | grep Index >/dev/null; then echo "There are changes, won't make a release!"; false; fi
	if test -e ../releases/showip_$(VERSION).xpi; then echo "Release already exists!"; false; fi
	mv showip_$(VERSION).xpi ../releases/
	cd ../releases && svn add showip_$(VERSION).xpi
	cd ../releases && svn ci -m "Release $(VERSION)" showip_$(VERSION).xpi
	svn copy -m "Release $(VERSION)" https://firefox-showip.googlecode.com/svn/trunk https://firefox-showip.googlecode.com/svn/release-$(VERSION)

releasepublic: version revision
	
revision:
	@echo $(REVISION)
	@make version
version:
	@echo showip_$(shell echo $(BETAVERSION)).xpi
# | sed s/\\./_/g).xpi

clean:
	rm ipv6ident.jar || true
	rm install.rdf || true
	rm install.js || true
	rm chrome.manifest || true
	mv *.xpi archive/ || true

help: 
	@echo "package channel={beta|public} <- builds a package"
